package com.jshx.sz.gdjt.util.file;

 

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class UploadMultiFileThread extends Thread {

	private Handler handler;
	private List<UploadItem> waitList;   // 待上传列表
	private boolean suspendUpload = false;  // 暂停上传
	private boolean stopUpload = false;     // 停止上传
	private int sendCount = 0;
	
	public static String MSG_SENDCOUNT = "send_count";
	public static String MSG_UNSENDCOUNT = "unsend_count";
	public static String MSG_ITEM = "upload_item";
	
	@SuppressWarnings("unused")
	private String TAG = "UploadMultiFileThread";
	
	public static final int NOTIFYCHANGE = 0xFFF002;
	public static final int UPLOADSUCC = 0xFFF003;
	public static final int UPLOADFAIL = 0xFFF004;

	public UploadMultiFileThread(Handler hand)
	{
		handler = hand;
		waitList = new ArrayList<UploadItem>();
	}
	
	public void add(UploadItem item)
	{
		waitList.add(item);
		
		synchronized(this)
		{
			this.notify();
		}
		
		// 发送列表变化，向主线程通知
		sendNotifyMessage();
	}
	
	public boolean isSuspended() {
		return suspendUpload;
	}

	/**
	 * 暂停上传线程
	 */
	public void suspendUpload() {
		this.suspendUpload = true;
		
		synchronized(this)
		{
			this.notify();
		}
	}
	
	public boolean isStopUpload() {
		return stopUpload;
	}

	/**
	 * 停止上传线程
	 */
	public void stopUpload() {
		this.stopUpload = true;
		
		synchronized(this)
		{
			this.notify();
		}
	}
	
	public void resumeUpload() {
		this.suspendUpload = false;
		
		synchronized(this)
		{
			this.notify();
		}
	}
	
	/**
	 * 上传完成后是否删除文件
	 */
	private boolean delFileAfterUpload = false;
	
	public boolean isDelFileAfterUpload() {
		return delFileAfterUpload;
	}

	public void setDelFileAfterUpload(boolean delFileAfterUpload) {
		this.delFileAfterUpload = delFileAfterUpload;
	}

	@Override
	public void run()
	{
		FileUploadUtils utils = new FileUploadUtils();
		
		while(stopUpload == false)
		{
			UploadItem item = null;
			try
			{
				// 如果待发送列表中已完成，则等待
				if(waitList.size() <= 0 || suspendUpload)
				{
					synchronized (this) {
						this.wait();
					}
				}
				
				item = waitList.get(0);
				utils.setFilePath(item.getPath());
				utils.setUploadUrl(item.getUrl());
				
				try
				{
					Map<String, List<String>> headers = utils.upload();
					
					if(null == headers)
					{
						// 把上传完成的照片从列表中删除
						waitList.remove(0);
						sendFailedMessage(item);
						continue;
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
					
					sendFailedMessage(item);
					
					waitList.remove(0);
					continue;
				}
				
				
				// 把上传完成的照片从列表中删除
				waitList.remove(0);
				// 通知主线程发送成功
				sendSuccMessage(item);
				
				// 删除文件
				if(delFileAfterUpload)
				{
					File file = new File(item.getPath());
					file.delete();
				}
				
				sendCount ++;
				
				// 向主线程发送通知
				sendNotifyMessage();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 通知主线程，发送列表产生变化
	 */
	private void sendNotifyMessage()
	{
		Message msg = handler.obtainMessage();
		msg.what = NOTIFYCHANGE;
		
		Bundle data = msg.getData();
		data.putInt(MSG_SENDCOUNT, sendCount);
		data.putInt(MSG_UNSENDCOUNT, waitList.size());
		handler.sendMessage(msg);
	}
	
	/**
	 * 通知主线程，发送成功
	 */
	private void sendSuccMessage(UploadItem item)
	{
		Message msg = handler.obtainMessage();
		msg.what = UPLOADSUCC;

		Bundle data = msg.getData();
		data.putParcelable(MSG_ITEM, item);
		handler.sendMessage(msg);
	}
	
	/**
	 * 通知主线程，发送失败
	 */
	private void sendFailedMessage(UploadItem item)
	{
		Message msg = handler.obtainMessage();
		msg.what = UPLOADFAIL;
		
		Bundle data = msg.getData();
		data.putParcelable(MSG_ITEM, item);
		handler.sendMessage(msg);
	}
}
